package com.slkj.shilixiaoyuanapp.ui.teacher.tool.PunchClock;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.ToolLocationEntity;
import com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.tkk.api.RxEventProcessor;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@ActivityInfo(layout = R.layout.activity_chose_pos_map, title = "位置", rightHint = "确定")
public class ChosePosMapActivity extends BaseActivity {
    @BindView(R.id.map_view)
    MapView mapView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    BaseQuickAdapter adapter;
    AMap aMap;
    double latitude,longitude;
    int chosedPos = -1;//选择的位置
    PoiSearch search;
    private List<ToolLocationEntity> posDatas=  new ArrayList<>();
    @Override
    protected void init() {
        initMap();
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseQuickAdapter<ToolLocationEntity, BaseViewHolder>(R.layout.item_tool_chodse_pos, posDatas) {
            @Override
            protected void convert(BaseViewHolder helper, final ToolLocationEntity item) {
                helper.setText(R.id.tv_title,item.getTitle());
                helper.setText(R.id.tv_content,item.getContent());
                helper.setVisible(R.id.img,item.isChoice());
            }
        };
        adapter.setOnItemClickListener((adapter, view, position) -> {
            if (chosedPos != position) {
                posDatas.get(position).setChoseChange();
                adapter.notifyItemChanged(position);
                if (chosedPos != -1) {
                    posDatas.get(chosedPos).setChoseChange();
                    adapter.notifyItemChanged(chosedPos);
                }
                chosedPos = position;
            }
        });
        recyclerView.setAdapter(adapter);
    }
    @Override
    protected void init(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
    }

    @Override
    protected void onRightTitleClick() {
        if (chosedPos == -1){
            showToast("请选择位置");
            return;
        }
        RxEventProcessor.get().post(RxEventProcessorTag.TOOL_CHOSE_POSTION_FINSH,posDatas.get(chosedPos));
        finish();
    }

    void initMap() {
        MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);
        aMap.getUiSettings().setZoomControlsEnabled(false);
        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        aMap.setMyLocationEnabled(true);
        aMap.setOnMyLocationChangeListener(aMapLocation -> {
            aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
            latitude = aMapLocation.getLatitude();
            longitude = aMapLocation.getLongitude();
            doSearchQuery();
        });
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
    }
    protected void doSearchQuery() {
        PoiSearch.Query query = new PoiSearch.Query("", "地名地址信息|公司企业|购物服务|交通设施服务|教育文化服务", "");
        query.setPageSize(20);
         search = new PoiSearch(this, query);
        query.setPageNum(1);
        search.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 300));
        search.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int code) {
                if (code == 1000){
                    List<ToolLocationEntity> data = new ArrayList<>();
                    int rangenStart = posDatas.size();
                    for (PoiItem item : poiResult.getPois()) {
                        LatLonPoint llp = item.getLatLonPoint();
                        double lon = llp.getLongitude();
                        double lat = llp.getLatitude();
                        String title = item.getTitle();
                        String content = item.getSnippet();
                        data.add(new ToolLocationEntity(lon, lat, title, content));
                    }
                    if (data.size() > 0) {
                        posDatas.addAll(data);
                        adapter.notifyItemRangeInserted(rangenStart,data.size()-2);
                    }
                }
            }
            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {
        
            }
        });
        search.searchPOIAsyn();

    }

    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        aMap.setOnMyLocationChangeListener(null);
        if (search != null){
            search.setOnPoiSearchListener(null);
        }
        super.onDestroy();

    }
}
